package cn.cms.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class App {
	private static boolean isWeb = false;
	private static Log log = LogFactory.getLog(App.class);
	
	private static String appPath=null;
	
		
	private static synchronized void pressAppPath(){
		if (null != appPath) return;
		String path = Path.getPathFromClass(App.class);
		if (log.isDebugEnabled()){
			log.debug("appPath class file path : "+path);
		}
		if (null == path || path.length()<1){
			throw new RuntimeException("get AppPath is null!");
		}
		path = path.replaceAll("\\\\", "/");
		int index = path.indexOf("/WEB-INF");
		if (index>-1){
			path = path.substring(0,index);
		}
		else {
			if (path.endsWith(".class")){
				String className = App.class.getName();
				className = className.replaceAll("\\.", "/")+".class";
				path = path.replaceAll(className, "");
			}
			else if(path.endsWith(".jar")) {
				path = path.substring(0,path.lastIndexOf('/'));					
			}
		}
		appPath = path;
	}

	public static String getAppPath() {
		if (null == appPath) pressAppPath();
		return appPath;
	}

	public static void setAppPath(String path) {
		if (null == path) return;
		if(path.endsWith("/")||path.endsWith("\\"))
		{
			appPath=path.substring(0,path.length()-1);
		}
		else {
			appPath = path;
		}
	}
	
	public static void setWebMark(){
		isWeb = true;
	}
	
	
	public static void main(String[] args){
		try{
			pressAppPath();
			System.out.println(appPath);
			
//			PathMatchingResourcePatternResolver p = new PathMatchingResourcePatternResolver();
//			System.out.println(p.getResourceLoader());
//			for(Resource r : p.getResources("file:d:/tmp/licl/*/*.xml")){
//				System.out.println(r.getFilename());
//			}
//			for(Resource r : p.getResources("file:d:/tmp/licl/licl_0.xml")){
//				System.out.println(r.getFilename());
//				r.getInputStream();
//			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}
